package org.limit.bookServe.vo;

import org.limit.bookServe.bean.User;
import org.limit.bookServe.utils.BaseClass;
import org.limit.bookServe.utils.SystemData;

import java.util.Objects;

import static org.limit.bookServe.bean.User.UserAccStatus.NORMAL;
import static org.limit.bookServe.enums.UserIdentityEnum.ADMIN;
import static org.limit.bookServe.enums.UserIdentityEnum.COMMON;
import static org.limit.bookServe.utils.SystemConstants.*;

/**
 * @author 1.
 * @version 1.0
 * @description: 主页ui
 * @date 2023/11/4 18:00
 */
public class MainUiVO extends BaseClass {
    @SuppressWarnings("ReassignedVariable")
    public void starter() {
        println(resourceReader(SHOW_INFO_TITLE));
        SystemData data = new SystemData();
        //载入系统数据
        boolean flag = data.loadingInfo();
        loop:
        while (flag) {
            //判断是否完成身份验证
            User currentUser;
            UserVO userVO = null;
            while ((currentUser = getCurrentUserInfo()) == null) {
                //登录注册
                println(resourceReader(SHOW_INFO_LOGIN));
                String select = input.nextLine();
                userVO = new UserVO(data.getUsers());
                switch (select) {
                    //登录
                    case "1":
                        userVO.login();
                        break;
                    //注册
                    case "2":
                        userVO.register();
                        break;
                    //退出
                    case "0":
                        break loop;
                }
            }
            //其他操作
            println("--------------------------------------------");
            if (currentUser.getStatus() == NORMAL) {
                println(currentUser.getUserName() + "，欢迎回来！");
                BookVO bookVO = new BookVO(data.getBooks());
                if (userVO == null) userVO = new UserVO(data.getUsers());
                if (currentUser.getIdentity() == ADMIN) {
                    //当前用户为 【管理员】
                    println(resourceReader(SHOW_ADMIN_SELECT));
                    String select0 = input.nextLine();
                    switch (select0) {
                        //用户管理
                        case "1":
                            String select1 = null;
                            while (select1 == null || !Objects.equals(select1, "0")) {
                                selectorShow0();
                                select1 = input.nextLine();
                                switch (select1) {
                                    //查找用户
                                    case "1":
                                        userVO.findUserInfo();
                                        break;
                                    //显示所有用户
                                    case "2":
                                        userVO.showAllUserAcc();
                                        break;
                                    //禁用用户
                                    case "3":
                                        userVO.bannedUserAcc();
                                        break;
                                    //修改用户帐号状态
                                    case "4":
                                        userVO.changedUserAccStatus();
                                        break;
                                    //退出【退出当前选择窗口】
                                    case "0":
                                        break;
                                }
                            }
                            break;
                        //书籍管理
                        case "2":
                            String select2 = null;
                            while (select2 == null || !Objects.equals(select2, "0")) {
                                selectorShow2();
                                select2 = input.nextLine();
                                switch (select2) {
                                    //添加书籍
                                    case "1":
                                        bookVO.addBookOne();
                                        break;
                                    //查找书籍
                                    case "2":
                                        bookVO.findBookOne();
                                        break;
                                    //删除书籍
                                    case "3":
                                        bookVO.deletedBookOne();
                                        break;
                                    //显示书籍
                                    case "4":
                                        bookVO.showAllBooks();
                                        break;
                                    //退出【退出当前选择窗口】
                                    case "0":
                                        break;
                                }
                            }
                            break;
                        case "0":
                            //退出【退出当前选择窗口】
                            exit();
                            break;
                    }
                } else if (currentUser.getIdentity() == COMMON) {
                    //当前用户为【普通用户】
                    selectorShow1();
                    String select0 = input.nextLine();
                    switch (select0) {
                        //查找书籍
                        case "1":
                            bookVO.findBookOne();
                            break;
                        //借阅书籍
                        case "2":
                            bookVO.borrowBookOne();
                            break;
                        //归还书籍
                        case "3":
                            bookVO.returningBook();
                            break;
                        //退出【退出当前选择窗口】
                        case "0":
                            exit();
                            break;
                    }
                }
            } else {
                println("cause：该账号异常");
                println();
                exit();
            }
        }
        println(resourceReader(SHOW_INFO_END));

    }

    //退出当前用户账号
    private void exit() {
        removeCurrentUser();
        println(resourceReader(SHOW_INFO_TITLE));
    }

    private void selectorShow2() {
        println("1. 添加书籍");
        println("2. 查找书籍");
        println("3. 删除书籍");
        println("4. 显示书籍");
        println("0. 退出");
        println("请选择: ");
    }

    private void selectorShow1() {
        println("1. 查找书籍");
        println("2. 借阅书籍");
        println("3. 归还书籍");
        println("0. 退出");
        println("请选择: ");
    }

    private void selectorShow0() {
        println("1. 查找用户");
        println("2. 显示用户");
        println("3. 禁用用户");
        println("4. 修改用户帐号状态 ");
        println("0. 退出");
        println("请选择: ");
    }

}
